package com.mvd.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kongzue.dialog.v3.WaitDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.adapter.OtherWaiJianPhotoAdapter;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.Constants;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.PDALogUtils;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.OtherWaiJianPhotoModel;
import com.mvd.app.model.PhotosModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 其他外检照片
 **/
public class OtherWaiJianPhotoActivity extends BaseActivity implements AdapterView.OnItemClickListener {


    @BindView(R.id.title_btn_left)
    ImageButton titleBtnLeft;
    @BindView(R.id.title_btn_left_txt)
    Button titleBtnLeftTxt;
    @BindView(R.id.title_txt)
    TextView titleTxt;
    @BindView(R.id.iv_headtip)
    ImageView ivHeadtip;
    @BindView(R.id.title_btn_right_txt)
    Button titleBtnRightTxt;
    @BindView(R.id.title_btn_right)
    ImageButton titleBtnRight;
    @BindView(R.id.ac_wj_photo_lv_main)
    ListView acWjPhotoLvMainListView;
    @BindView(R.id.activity_main)
    LinearLayout activityMain;
    @BindView(R.id.ac_waijian_btn_ok)
    Button acWaijianBtnOk;
    @BindView(R.id.ac_waijian_photo_ll_bottom_layout)
    LinearLayout acWaijianPhotoLlBottomLayout;
    @BindView(R.id.ac_waijian_photo_scrollview)
    ScrollView acWaijianPhotoScrollview;

    private OtherWaiJianPhotoActivity instances = null;
    private OtherWaiJianPhotoAdapter OtherWaiJianPhotoAdapter;
    private List<OtherWaiJianPhotoModel> buHeGeModel3List = null;
    private List<PhotosModel> photosModelList = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wai_jian_other_photo);
        ButterKnife.bind(this);
        instances = this;
        setBackBtn();
        String logStrf = "\n" + "onCreate-----其他外检照片---OtherWaiJianPhotoActivity-----" + "\n";
        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStrf.getBytes());


        buHeGeModel3List = new ArrayList<OtherWaiJianPhotoModel>();

        acWjPhotoLvMainListView.setOnItemClickListener(this);

        OtherWaiJianPhotoModel OtherWaiJianPhotoModel = null;
        for (int i = 0; i < Constants.WAIJIAN_PHOTO_LISTS.length; i++) {
            OtherWaiJianPhotoModel = new OtherWaiJianPhotoModel();
            OtherWaiJianPhotoModel.setCode(Constants.WAIJIAN_PHOTO_LISTS[i][1]);
            OtherWaiJianPhotoModel.setName(Constants.WAIJIAN_PHOTO_LISTS[i][0]);
            buHeGeModel3List.add(OtherWaiJianPhotoModel);
        }
        OtherWaiJianPhotoAdapter = new OtherWaiJianPhotoAdapter(instances, buHeGeModel3List, null);
        acWjPhotoLvMainListView.setAdapter(OtherWaiJianPhotoAdapter);
        setListViewHeightBasedONChildren(acWjPhotoLvMainListView);

//        WaitDialog.show(instances, "加载中...");
//        getOutPhotoItem();

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // TODO Auto-generated method stub
        // 取得ViewHolder对象
        OtherWaiJianPhotoAdapter.ViewHolder viewHolder = (OtherWaiJianPhotoAdapter.ViewHolder) view.getTag();
        if (viewHolder.mCheckBox.isChecked()) {
            viewHolder.mCheckBox.setChecked(false);
            Log.e("tag", position + "==取消了选择");
            buHeGeModel3List.get(position).setIsCheck("0");
        } else {
            viewHolder.mCheckBox.setChecked(true);
            buHeGeModel3List.get(position).setIsCheck("1");
            Log.e("tag", buHeGeModel3List.get(position).toString());
        }
        boolean check = viewHolder.mCheckBox.isChecked();
        OtherWaiJianPhotoAdapter.map.put(position, check);
//        for (int i = 0; i < buHeGeModel3List.size(); i++) {
//            if ("1".equals(buHeGeModel3List.get(i).getIsCheck())) {
//                BaseApplication.BaseApplication_OtherWaiJianPhotoModelList.add(buHeGeModel3List.get(i));
//
//            } else {
//
//            }
//
//        }
        if (OtherWaiJianPhotoAdapter.map.containsValue(false)) {
            //有选择的操作，即不是全部选中状态
            BaseApplication.IsAllSelect = false;
        }
        if (!OtherWaiJianPhotoAdapter.map.containsValue(false)) {
            BaseApplication.IsAllSelect = true;
        }
    }

    @OnClick({R.id.ac_waijian_btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_waijian_btn_ok:
                List<OtherWaiJianPhotoModel> list =new ArrayList<>();
                for (int i = 0; i < buHeGeModel3List.size(); i++) {
                    if ("1".equals(buHeGeModel3List.get(i).getIsCheck())) {
                        list.add(buHeGeModel3List.get(i));
                    }
                }

                Intent intent = new Intent();
                intent.putExtra("othet_waijian_photo", (Serializable) list);
                this.setResult(404, intent);
                this.finish();
                break;

        }
    }

    public static void setListViewHeightBasedONChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);

    }

    private void getOutPhotoItem() {
        UtilsLog.e("自选外检拍照列表---url==" + SharedPreferencesUtils.getIP(this) + OTHER_PHOTO);
        OkGo.<String>get(SharedPreferencesUtils.getIP(this) + OTHER_PHOTO).tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
//                        CommonUtils.showLoadingDialog(instances, "加载中...");

                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        photosModelList = new ArrayList<PhotosModel>();
                        String result = response.body();
                        BaseApplication.msg = result;
//                        UtilsLog.e("getOutPhotoItem-result==" + result);
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e("getOutPhotoItem-newResult==" + newResult);

                        String logStr = "\n" + "外检--自选外检拍照列表--请求成功" +
                                "\n" + "URL::" + SharedPreferencesUtils.getIP(instances) +
                                OTHER_PHOTO + "\n" + newResult;
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());

                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(result)) {

                            photosModelList = JsonUtil.stringToArray(newResult,
                                    PhotosModel[].class);
                            buHeGeModel3List = new ArrayList<OtherWaiJianPhotoModel>();
                            OtherWaiJianPhotoModel OtherWaiJianPhotoModel = null;
//        for (int i = 0; i < Constants.WAIJIAN_PHOTO_LISTS.length; i++) {
//            OtherWaiJianPhotoModel = new OtherWaiJianPhotoModel();
//            OtherWaiJianPhotoModel.setCode(Constants.WAIJIAN_PHOTO_LISTS[i][1]);
//            OtherWaiJianPhotoModel.setName(Constants.WAIJIAN_PHOTO_LISTS[i][0]);
//            buHeGeModel3List.add(OtherWaiJianPhotoModel);
//        }
//        OtherWaiJianPhotoAdapter = new OtherWaiJianPhotoAdapter(instances, buHeGeModel3List, null);
//        acWjPhotoLvMainListView.setAdapter(OtherWaiJianPhotoAdapter);
//        setListViewHeightBasedONChildren(acWjPhotoLvMainListView);
                            if (photosModelList != null && photosModelList.size() > 0) {
                                for (int i = 0; i < photosModelList.size(); i++) {
                                    if(photosModelList.get(i).getType().equals("1")){
                                        OtherWaiJianPhotoModel = new OtherWaiJianPhotoModel();
                                        OtherWaiJianPhotoModel.setCode(photosModelList.get(i).getPlatformCode());
                                        OtherWaiJianPhotoModel.setName(photosModelList.get(i).getName());
                                        buHeGeModel3List.add(OtherWaiJianPhotoModel);
                                    }
                                }
                                OtherWaiJianPhotoAdapter = new OtherWaiJianPhotoAdapter(instances, buHeGeModel3List, null);
                                acWjPhotoLvMainListView.setAdapter(OtherWaiJianPhotoAdapter);
                                setListViewHeightBasedONChildren(acWjPhotoLvMainListView);
                            }
                        } else {
                            String logStr2 = "\n" + "外检--自选外检拍照列表--失败" +
                                    "\n" + "result::" + result;
                            PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr2.getBytes());
                            BaseApplication.msg = result;
                        }
                        WaitDialog.dismiss();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        WaitDialog.dismiss();
                        BaseApplication.msg = response.body() + "==" + response.message();
                        UtilsLog.e("getOutPhotoItem-onError==" + response.body());
                        String logStr = "\n" + "外检--自选外检拍照列表-onError-失败" +
                                "\n" + "URL::" + SharedPreferencesUtils.getIP(instances) +
                                OTHER_PHOTO + "\n" + "result::" + response.body();
                        PDALogUtils.createLogFile(BaseApplication.JIANCE_MODE, logStr.getBytes());
                    }
                });
    }
}
